package figure8.GUI;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;

import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.sampled.Mixer;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.Border;

import figure8.Controller;
import figure8.DrumMachineTrack;
import figure8.Rec;
import figure8.events.PlaybackEvent;

public class DrumMachineSoundsWindow extends JDialog {
	private  int selectedItem;

	private File[] drums; 
	private File folder;
	
	public DrumMachineSoundsWindow(final Controller controller, final DrumMachineTrack track) {
		super();

		this.setLocation(controller.getWindow().getFrame().getX() + 80, controller.getWindow().getFrame()
				.getY() + 120);
		this.setIconImage(Toolkit.getDefaultToolkit().getImage("src/figure8/GUI/Fig8-small.png"));
		this.setAlwaysOnTop(true);

		Box column = Box.createVerticalBox();
		
		final DrumMachineSoundsWindow audioWin = this;

		folder = new File ("DrumSamples/");
		drums = folder.listFiles(new DrumFilter());
		Arrays.sort(drums);
		// Add the drop down list with it's elements
		Border rowBorder = BorderFactory.createEmptyBorder(5, 5, 5, 5);
		final JComboBox combo = new JComboBox(drums);
		combo.setBorder(rowBorder);

		controller.stop();
		JButton submit = new JButton("Submit");
		submit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				audioWin.dispose();
				selectedItem = combo.getSelectedIndex();
				track.changeKit(selectedItem);
				try {
					Thread.sleep(500);
				} catch (InterruptedException e1) {
					e1.printStackTrace();
				}
				controller.playback();
			}
		});
		column.add(combo);
		column.add(submit);
		JPanel settings = new JPanel();
		settings.add(column);
		settings.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		this.getContentPane().add(settings);
		this.pack();
		this.setVisible(true);
	}
	
	public int getMixer()
	{
		return selectedItem;
	}
	
	class DrumFilter implements FilenameFilter {
	    public boolean accept(File dir, String name) {
	        return !(name.equals(".svn"));
	    }
	}
}